//超长加密
// import {JSEncrypt as  JSEL} from './encryptlong/bin/jsencrypt.js';
//普通加密
import {JSEncrypt} from './jsencrypt/jsencrypt.min.js';
import {config} from "../config.js"

// //长加密
// export function rsa_longencode(str){
// 	var encrypt = new JSEL();
// 	encrypt.setPublicKey(config.rsa.public_key);
// 	return encrypt.encryptLong(JSON.stringify(str));
// }

// //长解密  
// export function rsa_longdecode(str){
// 	var decrypt = new JSEL();
// 	decrypt.setPrivateKey(config.rsa.private_key); 
// 	return decrypt.decryptLong(str); 
// }

 function substr(str,maxlen){
	var len = 0;
	for (var i=0; i<str.length; i++) {   
		 var c = str.charCodeAt(i);   
		 if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f))  
			len++;   
		 else  len+=3;
		 if(len>maxlen){
			 return str.substr(0,i)
		 }
	}   
	return str;  
}

//加密
export function rsa_encode(str){
	var encrypt = new JSEncrypt();
	encrypt.setPublicKey(config.rsa.public_key);
	return encrypt.encrypt(JSON.stringify(str));
}

//解密 
export function rsa_decode(str){
	var decrypt = new JSEncrypt();
	decrypt.setPrivateKey(config.rsa.private_key);
	return decrypt.decrypt(str); 
}

//生成密钥对
function generateKeys(){
  var crypt = new JSEncrypt({ default_key_size: 1024 });
  crypt.getKey();
  var publicKey = crypt.getPublicKey();
  var privateKey = crypt.getPrivateKey();
  // 去除-----*** RSA **** KEY----- 和空格换行
  publicKey = (publicKey.split('-----'))[2];
  publicKey = publicKey.replace(/\n/g, "").replace(/\r/g, "").replace(/\t/g, "").replace(/\s*/g, "");
  privateKey = (privateKey.split('-----'))[2];
  privateKey = privateKey.replace(/\n/g, "").replace(/\r/g, "").replace(/\t/g, "").replace(/\s*/g, "");
  // 返回生成的秘钥对
  return [publicKey, privateKey];
}

